
import request from '@/api/axios'
import {AxiosPromise} from 'axios'
import {DataRes} from '@/api/config'
import {DocDefinition, pageSizeHash} from '@/util/pdf'
import {setCnMedicine, transDate, getBaseDoc, getNormalHeader, getNormalFooter} from '@/api/report/physicalReport'
import { string } from 'mathjs'
export interface SearchIdentificationData{
  dutyDoctorId:string|null //体检医生id
  dutyDoctorName:string|null //体检医生姓名
  evaluationTime:string|null//评估日期
  result:string //体质类型结果
}

// 保存信息
export interface SaveInfor{
  id?:number //当前id
  checkupRegisterId:string//体检登记表id
  evaluationTime:string|null//评估日期
  constitution:string //体质辨识
  result:string //体质类型结果
  guideWriteValue:string//guideWriteValue
  questions:string//题目信息
  guideValue:string//健教指导内容
  dutyDoctorId:string|null //体检医生id
  dutyDoctorName:string|null //体检医生姓名
  checkupStatus:string //体检状态
  synchStatus:string //同步状态
}

// 基础问题数据
export interface BaseQuestions{
  title:string //主标题
  option:number //选项
  helpTitle?:string //副标题
}

//保存体质辨识
export const saveIdentificationInfor = (data:SaveInfor):AxiosPromise<DataRes<SaveInfor>> => {
  return request({
    url:'/api/tcmConstitutionIdentification/saveOrUpdate',
    method:'post',
    data
  })
}

// 健教指导树定义
export interface HelpView {
  version?:string //版本
  label:string
  id?:string
  disabled?:boolean
  children?:Array<HelpView>
}

export interface GuideDetailInfor {
  guideCheckedList: Array<string> // id
}

export interface HealthValue{
  id:string
  healthValue:string
}
export interface View{
  id:string
  smallTypeName:string
  healthContentList:Array<HealthValue>
}
export interface GuideView{
  id:string
  name:string
  smallTypeList: Array<View>
}

// 获取健教指导树
export const gethealthGuideTreeList = ():AxiosPromise<DataRes<Array<GuideView>>> => {
  return request({
    url:'/api/healthGuide/tcmIdentificationList',
    method:'get'
  })
}
//体质辨识详情
export const getcheckupRegisterList = (checkupRegisterId:string):AxiosPromise<DataRes<SaveInfor>> => {
  return request({
    url:`/api/tcmConstitutionIdentification/checkupRegister/${checkupRegisterId}/detail`,
    method:'get'
  })
}

export const getReportDoc = (data) => {
  const doc = getBaseDoc()
  doc.header = (currentPage: number, pageCount: number) => getNormalHeader(data)
  doc.footer = (currentPage: number, pageCount: number) => getNormalFooter(currentPage, pageCount, data)
  //转换日期
  transDate(data)
  //设置中医
  setCnMedicine(doc, data.tcmConstitutionIdentificationVO)
  return doc
}


const getPt = (data:number) => {
  return data * 0.75
}
//根据数据获取体质报告
export const getIdentificationDoc = (data) => {
  // 体检引导单页面
  const guideSheetDoc: DocDefinition = {
    //页面尺寸
    pageSize: pageSizeHash['A4'],
    //页面外边距
    pageMargins: [getPt(32), getPt(46), getPt(32), getPt(50)],
    //纸张方向 纵向(默认)或横线
    pageOrientation: 'portrait',
    //页眉元素
    header:[
      {
        columns: [
          {
            text: `体检号：${data.checkupNo || ''}`,
            width: getPt(207)
          },
          {
            text: `姓名：${data.personName || ''}`,
            width: getPt(146)
          },
          {
            text: `性别：${data.gender || ''}`,
            width: getPt(123)
          },
          {
            text: `年龄：${data.age || ''}岁`,
            width: getPt(124)
          },
          {
            text: `体检日期：${data.evaluationTime || ''}`,
            width: getPt(130)
          }
        ],
        absolutePosition:{x:getPt(32), y:getPt(20)},
        columnGap: getPt(2),
        fontSize: getPt(12)
      },
      {
        canvas:[{
          type: 'rect',
          x: getPt(32),
          y: getPt(44),
          w: getPt(730),
          h: getPt(2),
          color: '#000000'
        }]
      }
    ],
    //元素列表
    content: [
      {
        text: '【中医体质报告】',
        width: getPt(144),
        height: getPt(27),
        fontSize:getPt(18),
        bold:true,
        margin:[0, getPt(5), 0, getPt(5)]
      },
      {
        canvas:[{
          type: 'rect',
          x: getPt(0),
          y: getPt(0),
          w: getPt(730),
          h: getPt(2),
          color: '#000000'
        }]
      },
      {
        text: `体质类型：${data.result || ''}`,
        width: getPt(730),
        height: getPt(53),
        fontSize:getPt(14),
        margin:[0, getPt(20), 0, getPt(20)]
      }
    ],
    //页脚元素
    footer:(currentPage: number, pageCount: number) => ([
      {
        canvas:[{
          type: 'rect',
          x: getPt(32),
          y: getPt(0),
          w: getPt(730),
          h: getPt(2),
          color: '#000000'
        }]
      }, {
        columns: [
          {
            text: `打印时间：${data.printDate || ''}`,
            width: getPt(250),
            alignment:'left'
          },
          {
            text: currentPage + '/' + pageCount,
            width: getPt(230),
            alignment:'center'
          },
          {
            text: data.orgName || '',
            width: getPt(250),
            alignment:'right'
          }
        ],
        absolutePosition:{x:getPt(32), y:getPt(12)},
        columnGap: getPt(2),
        fontSize: getPt(12)
      }
    ]),
    // 默认样式
    defaultStyle: {font: 'SourceHanSansCN'}
  }
  data.guideDataList.map((item) => {
    if (item.label) guideSheetDoc.content.push({
      text: `【${item.label}】`,
      width: getPt(730),
      height: getPt(16),
      fontSize:getPt(14)
    })
    item.children.map((ite) => {
      if (ite.label)guideSheetDoc.content.push({
        text: ite.label,
        width: getPt(730),
        height: getPt(16),
        fontSize:getPt(14)
      })
      ite.children.map((its) => {
        if (ite.label)guideSheetDoc.content.push({
          text: its.label,
          width: getPt(730),
          height: getPt(16),
          fontSize:getPt(14)
        })
      })
    })
  })
  if (data.guideWriteValue)guideSheetDoc.content.push({
    text: data.guideWriteValue,
    width: getPt(730),
    height: getPt(16),
    fontSize:getPt(14),
    margin:[0, data.guideDataList.length ? getPt(16) : 0, 0, 0]
  })
  guideSheetDoc.content.push({
    text: `检查医生：${data.dutyDoctorName || ''}`,
    width: getPt(730),
    height: getPt(16),
    fontSize:getPt(14),
    alignment:'right',
    margin:[0, getPt(54), 0, 0]
  })
  return guideSheetDoc
}

